package com.cyt.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cyt.dao.impl.SPFeiLeiDaoImpl;
import com.cyt.dao.impl.XingWenFengLeiDaoImpl;
import com.cyt.entity.SPFeiLei;
import com.cyt.entity.XingWenFengLei;
import com.cyt.util.BaseServlet;
import com.cyt.util.Result;
import com.google.gson.Gson;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;





public class AdminChanPinFLServlet extends BaseServlet {
	
	
	
	/**
	 * 查询所有
	 * localhost:8080/shiping/adminChanPinFL?reqName=getChanPinFL
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getChanPinFL(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	     SPFeiLeiDaoImpl spDaoi=new SPFeiLeiDaoImpl();//创建产品分类的实现类对象
		    
	     List <SPFeiLei> spLists= spDaoi.getSPFeiLei();//查到的所有产品分类
	
	
	
	     response.setCharacterEncoding("UTF-8");//设置响应编码格式
	     PrintWriter out=response.getWriter();//创建out对象
	
	     Gson gson=new Gson();
	     String res =gson.toJson(spLists);//将spLists里面的数据转换为json字符串
	     
	     out.print(res);//响应数据
	     
	     out.flush();
	     out.close();
	}
	/**
	 * 查询指定条数产品分类 分页
	 * @author 喜悦
	 *localhost:8080/shiping/adminChanPinFL?reqName=getChanPinFLTS
	 */
	public void getChanPinFLTS(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	     SPFeiLeiDaoImpl spDaoi=new SPFeiLeiDaoImpl();//创建产品分类的实现类对象
		    int page=Integer.parseInt(request.getParameter("page"));//接收前台传来的参数
		    int size=Integer.parseInt(request.getParameter("size"));//接收前台传来的参数

	     List <SPFeiLei> spLists= spDaoi.getSPFeiLeiPage(page, size);//查到的所有产品分类
	
	//发送ajax请求,服务器基本上都返回json格式字符串
	
	     response.setCharacterEncoding("UTF-8");//设置响应编码格式
	     PrintWriter out=response.getWriter();//创建out对象
	
	     Gson gson=new Gson();
	     String res =gson.toJson(spLists);//将spLists里面的数据转换为json字符串
	     
	     out.print(res);//响应数据
	     
	     out.flush();
	     out.close();
	}
 
	/**
	 * localhost:8080/shiping/adminChanPinFL?reqName=getChanPinFLTiao
	 * 查询所有表中数据条数
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getChanPinFLTiao(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	     SPFeiLeiDaoImpl spDaoi=new SPFeiLeiDaoImpl();//创建产品分类的实现类对象
		    
	     List <SPFeiLei> spLists= spDaoi.getSPFeiLei();//查到的所有产品分类
	
	//发送ajax请求,服务器基本上都返回json格式字符串
	
	     response.setCharacterEncoding("UTF-8");//设置响应编码格式
	     PrintWriter out=response.getWriter();//创建out对象
	
	     Gson gson=new Gson();
	     String res =gson.toJson(spLists.size());//将spLists里面的数据转换为json字符串
	     
	     out.print(res);//响应数据
	     
	     out.flush();
	     out.close();
	}
	
	/**
	 * 删除
	 * localhost:8080/shiping/adminChanPinFL?reqName=shanChuChanPinFL
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void shanChuChanPinFL(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    int cId=Integer.parseInt(request.getParameter("categoryId"));//接收前台传来的参数
		SPFeiLeiDaoImpl spDaoi=new SPFeiLeiDaoImpl();//创建产品分类的实现类对象
	    int n =spDaoi.shanChuChanPinFL(cId);//调用删除的方法,获得数据库受影响行数n
	    response.setCharacterEncoding("UTF-8");
	    PrintWriter out=response.getWriter();
	    if (n>0) {//删除成功
			out.print(Result.toClient(true, "删除成功", ""));
		} else {//删除失败
			out.print(Result.toClient(false, "删除失败", ""));

		}
	     
	    out.flush();
	    out.close();
	}
	
	/**
	 * localhost:8080/shiping/adminChanPinFL?reqName=shanChuChanPinFLs
	 * 批量删除
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void shanChuChanPinFLs(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
	    PrintWriter out=response.getWriter();
	    SPFeiLeiDaoImpl spDaoi=new SPFeiLeiDaoImpl();
		String ids=request.getParameter("ids");
		
	    int n =spDaoi.shanChanPinFLS(ids);
	    
	    if (n>0) {//删除成功
			out.print(Result.toClient(true, "删除成功", ""));
		} else {//删除失败
			out.print(Result.toClient(false, "删除失败", ""));

		}
	     
	    out.flush();
	    out.close();
	}
	
	/**
	 * 添加产品
	 * localhost:8080/shiping/adminChanPinFL?reqName=AddChanPinFL
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void AddChanPinFL(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
	    PrintWriter out=response.getWriter();
	    SPFeiLeiDaoImpl SPFeiDaoi=new SPFeiLeiDaoImpl();
		String sPTuPian=request.getParameter("sPTuPian");

		String sPFengLeiName=request.getParameter("sPFengLeiName");
		if(sPFengLeiName !=null && !sPFengLeiName.equals("")) {
	    List<SPFeiLei> cpList=SPFeiDaoi.getCPFLName(sPFengLeiName);
	    if(cpList.size()==0) {
	    	SPFeiLei spf= new SPFeiLei(0, sPTuPian, sPFengLeiName);
			
			int n= SPFeiDaoi.addChanPinFL(spf);
	        System.out.println(spf);
			
		    if (n>0) {
				out.print(Result.toClient(true, "添加成功", ""));
			} else {
				out.print(Result.toClient(false, "添加失败", ""));

			}
	    }else{
			out.print(Result.toClient(false, "系列名已存在", ""));

	    }
		
	}
		
	     
	    out.flush();
	    out.close();
	}
	
	/**
	 *上传图片
	 * localhost:8080/shiping/adminChanPinFL?reqName=upload
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void upload(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out =response.getWriter();
		
		//1.创建SmartUpload对象
		SmartUpload smart=new SmartUpload();
		//2.设置编码格式
		smart.setCharset("UTF-8");
		//3.初始化
		smart.initialize(getServletConfig(), request, response);
		
		try {//正常
			// 4.上传
			smart.upload();
			// 5.设置上传文件名字
			String fileName =System.currentTimeMillis()+".jpg";
			// 6.将文件保存到指定目录
			smart.getFiles().getFile(0).saveAs("images/"+fileName);
			// 7.响应数据
			out.print(Result.toClient(true, "上传成功", fileName));
		} catch (SmartUploadException e) {//异常
			// TODO Auto-generated catch block
			e.printStackTrace();
			out.print(Result.toClient(false, "上传失败", "图片过大"));

		}
		
	}
	
	/**
	 * localhost:8080/shiping/adminChanPinFL?reqName=chaChanPinFLId
	 * 通过id查数据,前台用于数据回显
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void chaChanPinFLId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
	    PrintWriter out=response.getWriter();
	    
	    SPFeiLeiDaoImpl spflDaoi=new SPFeiLeiDaoImpl();
		int spflId=Integer.parseInt(request.getParameter("sPFengLeiId"));
		SPFeiLei spfl= spflDaoi.chaSPFeiLeiId(spflId);
        
		Gson gson=new Gson();
		String res =gson.toJson(spfl);
	    out.print(res);
	    
	    
	    out.flush();
	    out.close();
	}
	
	
	/**
	 * 修改产品系列
	 * localhost:8080/shiping/adminChanPinFL?reqName=xiuGaiChanPinFL
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void xiuGaiChanPinFL(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
	    PrintWriter out=response.getWriter();
	    
	    SPFeiLeiDaoImpl xinWenDaoi=new SPFeiLeiDaoImpl();
		int sPFengLeiId=0;
		if(request.getParameter("sPFengLeiId")!=null && !request.getParameter("sPFengLeiId").equals("")) {
			sPFengLeiId=Integer.parseInt(request.getParameter("sPFengLeiId"));
		}
		String sPTuPian=request.getParameter("sPTuPian");

		String sPFengLeiName=request.getParameter("sPFengLeiName");
		if(sPFengLeiName !=null && !sPFengLeiName.equals("")) {
			List<SPFeiLei> cpList=xinWenDaoi.getCPFLName(sPFengLeiName);
			if(cpList.size()==0) {
				SPFeiLei spfl= new SPFeiLei(sPFengLeiId, sPTuPian, sPFengLeiName);
				int n =xinWenDaoi.xiuGaiSPFeiLei(spfl);//调用删除的方法,获得数据库受影响行数n
			    
			    if (n>0) {//删除成功
					out.print(Result.toClient(true, "修改成功", ""));
				} else {//删除失败
					out.print(Result.toClient(false, "修改失败", ""));

				}
				
			}else{
				out.print(Result.toClient(false, "系列名已存在", ""));
		    
			}
		}
	    out.flush();
	    out.close();
	}
	
}
